Merge pull request #681 from ianblenke/docker_supervisord

Docker supervisord

Andrew Cantino 10 years ago
parent
commit
d072e74ba2
2 changed files with 93 additions and 24 deletions
  1. 3 1
      docker/Dockerfile
  2. 90 23
      docker/scripts/init

+ 3 - 1
docker/Dockerfile

@@ -13,10 +13,12 @@ RUN apt-get update && \
13 13
       libgdbm-dev libreadline-dev libncurses5-dev libffi-dev \
14 14
       libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev \
15 15
       graphviz libgraphviz-dev \
16
-      ruby2.1 ruby2.1-dev supervisor && \
16
+      ruby2.1 ruby2.1-dev supervisor python-pip && \
17 17
     gem install --no-ri --no-rdoc bundler && \
18 18
     rm -rf /var/lib/apt/lists/*
19 19
 
20
+RUN pip install supervisor-stdout
21
+
20 22
 ADD scripts/ /scripts
21 23
 RUN chmod 755 /scripts/setup /scripts/init
22 24
 

+ 90 - 23
docker/scripts/init

@@ -19,7 +19,7 @@ elif [ -n "${POSTGRESQL_PORT_5432_TCP_ADDR}" ]; then
19 19
   HUGINN_DATABASE_PORT=${HUGINN_DATABASE_PORT:-${POSTGRESQL_PORT_5432_TCP_PORT}}
20 20
 fi
21 21
 
22
-grep = /app/.env.example | sed -e 's/^#[^ ]//' | grep -v -e '^#' | cut -d= -f1 | \
22
+grep = /app/.env.example | sed -e 's/^#\([^ ]\)/\1/' | grep -v -e '^#' | cut -d= -f1 | \
23 23
   while read var ; do
24 24
     eval "echo \"$var=\\\"\${HUGINN_$var:-\$$var}\\\"\""
25 25
   done | grep -v -e ^= > /app/.env
@@ -38,12 +38,35 @@ case "${DATABASE_ADAPTER}" in
38 38
   *) echo "Unsupported database adapter. Available adapters are mysql2, and postgres." && exit 1 ;;
39 39
 esac
40 40
 
41
-# start supervisord
42
-/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
41
+# initialize supervisord config
42
+
43
+cat > /etc/supervisor/conf.d/supervisord.conf <<EOF
44
+[supervisord]
45
+nodaemon = true
46
+user = root
47
+stdout_logfile = AUTO
48
+
49
+[unix_http_server]
50
+file=/var/run/supervisor.sock   ; (the path to the socket file)
51
+
52
+[eventlistener:stdout]
53
+command = supervisor_stdout
54
+buffer_size = 100
55
+events = PROCESS_LOG
56
+result_handler = supervisor_stdout:event_handler
57
+EOF
58
+
59
+# prepare the supervisord bootstrap service script
60
+
61
+cat <<BOOTSTRAP > /tmp/bootstrap.sh
62
+#!/bin/bash -e
63
+source /app/.env
64
+
65
+echo DATABASE_HOST=\${DATABASE_HOST}
43 66
 
44
-# start mysql server if ${DATABASE_HOST} is localhost
45
-if [ "${DATABASE_HOST}" == "localhost" ]; then
46
-  if [ "${DATABASE_ADAPTER}" == "postgres" ]; then
67
+# start mysql server if \${DATABASE_HOST} is the .env.example default
68
+if [ "\${DATABASE_HOST}" = "your-domain-here.com" -o "\${DATABASE_HOST}" = "" ]; then
69
+  if [ "\${DATABASE_ADAPTER}" = "postgres" ]; then
47 70
     echo "DATABASE_ADAPTER 'postgres' is not supported internally. Please provide DATABASE_HOST."
48 71
     exit 1
49 72
   fi
@@ -57,10 +80,11 @@ command=/usr/bin/mysqld_safe
57 80
 user=root
58 81
 autostart=false
59 82
 autorestart=true
60
-stdout_logfile=/var/log/supervisor/%(program_name)s.log
61
-stderr_logfile=/var/log/supervisor/%(program_name)s.log
83
+stdout_events_enabled=true
84
+stderr_events_enabled=true
62 85
 EOF
63
-  supervisorctl reload
86
+  supervisorctl reread
87
+  supervisorctl add mysqld
64 88
 
65 89
   # fix permissions and ownership of /var/lib/mysql
66 90
   chown -R mysql:mysql /var/lib/mysql
@@ -76,36 +100,79 @@ EOF
76 100
 
77 101
   # wait for mysql server to start (max 120 seconds)
78 102
   timeout=120
79
-  while ! mysqladmin -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD} status >/dev/null 2>&1
103
+  while ! mysqladmin -uroot \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD} status >/dev/null 2>&1
80 104
   do
81
-    timeout=$(expr $timeout - 1)
82
-    if [ $timeout -eq 0 ]; then
105
+    (( timeout = timeout - 1 ))
106
+    if [ \$timeout -eq 0 ]; then
83 107
       echo "Failed to start mysql server"
84 108
       exit 1
85 109
     fi
110
+    echo -n .
86 111
     sleep 1
87 112
   done
88 113
 
89
-  if ! echo "USE ${DATABASE_NAME}" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD} >/dev/null 2>&1; then
114
+  if ! echo "USE \${DATABASE_NAME}" | mysql -uroot \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD} >/dev/null 2>&1; then
90 115
     DB_INIT="yes"
91
-    echo "CREATE DATABASE IF NOT EXISTS \`${DATABASE_NAME}\` DEFAULT CHARACTER SET \`utf8\` COLLATE \`utf8_unicode_ci\`;" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD}
92
-    echo "GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON \`${DATABASE_NAME}\`.* TO 'root'@'localhost';" | mysql -uroot ${DATABASE_PASSWORD:+-p$DATABASE_PASSWORD}
116
+    echo "CREATE DATABASE IF NOT EXISTS \\\`\${DATABASE_NAME}\\\` DEFAULT CHARACTER SET \\\`utf8\\\` COLLATE \\\`utf8_unicode_ci\\\`;" | mysql -uroot \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD}
117
+    echo "GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON \\\`${DATABASE_NAME}\\\`.* TO 'root'@'localhost';" | mysql -uroot \${DATABASE_PASSWORD:+-p\$DATABASE_PASSWORD}
93 118
   fi
94 119
 fi
120
+supervisorctl start foreman >/dev/null
121
+BOOTSTRAP
122
+
123
+chmod 755 /tmp/bootstrap.sh
124
+
125
+cat > /etc/supervisor/conf.d/bootstrap.conf <<EOF
126
+[program:bootstrap]
127
+command=/tmp/bootstrap.sh
128
+priority=10
129
+directory=/app
130
+process_name=%(program_name)s
131
+autostart=true
132
+autorestart=false
133
+stdout_events_enabled=true
134
+stderr_events_enabled=true
135
+stopsignal=TERM
136
+exitcodes=0
137
+startsecs=0
138
+stopwaitsecs=1
139
+EOF
140
+
141
+cat <<FOREMAN > /tmp/foreman.sh
142
+#!/bin/bash -e
143
+source /app/.env
95 144
 
96 145
 # Assuming we have a created database, run the migrations and seed it idempotently.
97
-[ -z "${DO_NOT_MIGRATE}" ] && sudo -u huginn -EH bundle exec rake db:migrate
98
-[ -z "${DO_NOT_SEED}" ] && sudo -u huginn -EH bundle exec rake db:seed
146
+[ -z "\${DO_NOT_MIGRATE}" ] && sudo -u huginn -EH bundle exec rake db:migrate
147
+[ -z "\${DO_NOT_SEED}" ] && sudo -u huginn -EH bundle exec rake db:seed
99 148
 
100
-[ -n "$INTENTIONALLY_SLEEP" ] && sleep $INTENTIONALLY_SLEEP
149
+[ -n "\$INTENTIONALLY_SLEEP" ] && sleep \$INTENTIONALLY_SLEEP
101 150
 
102 151
 # Fixup the Procfile and prepare the PORT
103
-[ -z "${DO_NOT_RUN_JOBS}" ] && perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
104
-perl -pi -e 's/rails server$/rails server -p \$PORT/' /app/Procfile
152
+[ -n "\${DO_NOT_RUN_JOBS}" ] && perl -pi -e 's/^jobs:/#jobs:/' /app/Procfile
153
+perl -pi -e 's/rails server\$/rails server -p \\\$PORT/' /app/Procfile
105 154
 export PORT
106 155
 
107 156
 # Start huginn
108
-sudo -u huginn -EH bundle exec foreman start
157
+exec sudo -u huginn -EH bundle exec foreman start
158
+FOREMAN
159
+
160
+chmod 755 /tmp/foreman.sh
109 161
 
110
-# As the ENTRYPOINT script, when this exits the docker container will Exit.
111
-exit 0
162
+cat > /etc/supervisor/conf.d/foreman.conf <<EOF
163
+[program:foreman]
164
+command=/tmp/foreman.sh
165
+priority=10
166
+directory=/app
167
+process_name=%(program_name)s
168
+autostart=false
169
+autorestart=false
170
+stdout_events_enabled=true
171
+stderr_events_enabled=true
172
+stopsignal=TERM
173
+startsecs=0
174
+stopwaitsecs=1
175
+EOF
176
+
177
+# start supervisord
178
+exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf